<script setup>
import {
  getEnterpriseAll,
  addEnterprise,
  delEnterprise,
  getEnterpriseById,
  updateEnterprise
} from "@/api/system/enterprise/enterpriseXx"
import {listTree} from "@/api/system/park/building"

export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 3,
        employeeName: "",
        employeePhone: "",
        enterpriseName: ""
      }, ruleForm: {
        id: "",
        bulidingId: "",
        floorId: "",
        roomId: ""

      }, rules: {
        enterpriseName: [
          {required: true, message: '请输入企业名称姓名', trigger: 'blur'},
        ],
        enterprisePrincipal: [
          {required: true, message: '请输入企业负责人', trigger: 'blur'},
        ]
      },
      bulidingTree: [],
      floorList: [],
      roomList: [],
      dialogVisible: false,
      total: 1,
      tableData: []
    }
  }, created() {
    let that = this;
    that.queryMethods();
    listTree().then(function (data) {
      that.bulidingTree = data.data;
    })
  }, methods: {
    queryMethods() {
      let that = this;
      getEnterpriseAll(that.queryParams).then(function (data) {
        that.tableData = data.data.list
        that.queryParams.pageNum = data.data.pageNum;
        that.queryParams.pagSize = data.data.pageSize;
        that.total = data.data.total;
      })
    },
    resetQuery() {
      this.queryParams = {};
      this.resetForm("queryParams");
      this.queryMethods();
    },
    //树改变
    changBuliding() {
      let that = this;
      that.floorList = null;
      that.ruleForm.floorId = "";
      that.roomList = null;
      that.ruleForm.roomId = "";

      let chooseBulidingId = that.ruleForm.bulidingId;

      for (let i = 0; i < that.bulidingTree.length; i++) {
        if (that.bulidingTree[i].id == chooseBulidingId) {
          that.floorList = that.bulidingTree[i].children;
        }
      }
    },
    changFloor() {
      let that = this;

      that.roomList = null;
      that.ruleForm.roomId = "";

      let chooseFloorId = that.ruleForm.floorId;

      for (let i = 0; i < that.floorList.length; i++) {
        if (that.floorList[i].id == chooseFloorId) {
          that.roomList = that.floorList[i].children;
        }
      }

    },
    addEnterpriseShow() {
      this.dialogVisible = true;
      this.ruleForm = {
        id: "",
        bulidingId: "",
        floorId: "",
        roomId: ""
      }
    },
    delExamine(id) {
      let that = this;
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delEnterprise(id).then(function (data) {
          if (data.code == 200) {
            that.$message.success("删除成功");
          } else {
            that.$message.error("删除失败")
          }
          that.queryMethods();
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    submitForm(formName) {
      let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.ruleForm.id == "" || that.ruleForm.id == undefined) {
            addEnterprise(that.ruleForm).then((res) => {
              if (res.code == 200) {
                that.$message.success("添加成功");
              } else {
                that.$message.error("添加失败")
              }
              that.dialogVisible = false;
              that.queryMethods();
            })
          } else {
            updateEnterprise(that.ruleForm).then((res) => {
              if (res.code == 200) {
                that.$message.success("修改成功");
              } else {
                that.$message.error("修改失败")
              }
              that.dialogVisible = false;
              that.queryMethods();
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    updateShow(id) {
      let that = this;
      getEnterpriseById(id).then(function (data) {
        that.ruleForm = data.data;
        that.ruleForm.bulidingId = data.data.bulidingId;
        that.ruleForm.floorId = data.data.floorId;
        that.ruleForm.roomId = data.data.roomId;

      })
      that.dialogVisible = true;
    }, handleSizeChange(pageSize) {

      this.queryParams.pageSize = pageSize;
      this.queryMethods();
    },
    handleCurrentChange(pageNum) {
      this.queryParams.pageNum = pageNum;
      this.queryMethods();
    }
  }

}
</script>

<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="企业名称">
        <el-input
          v-model="queryParams.employeeName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="企业负责人">
        <el-input
          v-model="queryParams.employeePhone"
          placeholder="请输入企业负责人"
          clearable
          size="small"
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="办公地址">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入办公地址"
          clearable
          size="small"
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryMethods()">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" size="mini" @click="addEnterpriseShow()">新增</el-button>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%">
      <el-table-column
        prop="id"
        label="序号"
        align="center"
        width="80">
      </el-table-column>
      <el-table-column
        prop="enterpriseName"
        label="企业名称"
        align="center"
        width="120">
      </el-table-column>
      <el-table-column
        prop="enterprisePrincipal"
        label="企业负责人"
        align="center"
        width="150">
      </el-table-column>
      <el-table-column
        prop="enterprisePrincipalPhone"
        label="企业负责人电话"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="address"
        label="办公地址"
        align="center"
        width="170">
      </el-table-column>
      <el-table-column
        prop="enterpriseDescribe"
        label="企业描述"
        align="center"
        width="200">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="enterTime"
        label="入住时间"
        align="center"
        width="110">
      </el-table-column>
      <el-table-column
        prop="status"
        label="企业状态"
        align="center"
        width="110">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="230">
        <template slot-scope="scope">
          <el-tooltip content="编辑">
            <el-button type="primary" icon="el-icon-edit" @click="updateShow(scope.row.id)" circle></el-button>
          </el-tooltip>
          <el-tooltip content="删除">
            <el-button type="danger" icon="el-icon-delete" @click="delExamine(scope.row.id)" circle></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryParams.pageNum"
        :page-size="this.queryParams.pageSize"
        :page-sizes="[1,2,3]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      title="添加企业信息"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="ruleForm.enterpriseName"></el-input>
        </el-form-item>
        <el-form-item label="企业负责人" prop="enterprisePrincipal">
          <el-input v-model="ruleForm.enterprisePrincipal"></el-input>
        </el-form-item>
        <el-form-item label="企业负责人电话">
          <el-input v-model="ruleForm.enterprisePrincipalPhone"></el-input>
        </el-form-item>
        <el-form-item label="申请地址">
          <el-select
            @change="changBuliding"
            v-model="ruleForm.bulidingId"
            placeholder="楼栋类型"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option v-for="o in bulidingTree"
                       :key="o.buildingName"
                       :label="o.buildingName"
                       :value="o.id"

            ></el-option>
          </el-select>
          <el-select
            @change="changFloor"
            v-model="ruleForm.floorId"
            placeholder="楼层"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option v-for="o in floorList"
                       :key="o.floorName"
                       :label="o.floorName"
                       :value="o.id"

            ></el-option>
          </el-select>
          <el-select
            v-model="ruleForm.roomId"
            placeholder="房间"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option v-for="o in roomList"
                       :key="o.roomName"
                       :label="o.roomName"
                       :value="o.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="企业描述">
          <el-input v-model="ruleForm.enterpriseDescribe"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">操作</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">

</style>
